//
//  SDNavigationBar.m
//  SKinDiary
//
//  Created by pcyang on 16/6/26.
//  Copyright © 2016年 tencent. All rights reserved.
//

#import "SDNavigationBar.h"

#define NavBackButtonTag 20016

#define navBottomGreenImgTag 20017

#define NavEditButtonTag 20018


#define kNaviTitleLabelMargin       40
#define kNaviTitleLabelHeight       44

#define kNaviEditButtonMarginLeft    5
#define kNaviEditButtonMarginTop    5
#define kNaviEditButtonMarginRight  8

#define kNaviEditButtonWidth        60
#define kNaviEditButtonHeight       44


@interface SDNavigationBar()
{
}
@end

@implementation SDNavigationBar

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

- (instancetype)init
{
    return [self initWithFrame:CGRectMake(0, 0, kScreenWidth, kNavBarHeight)];
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        UIView* bottomLine = [[UIView alloc] initWithFrame:CGRectMake(0, self.height - 0.5, self.width, 0.5)];
       // bottomLine.backgroundColor = [UIColor commenLineColor];
        [self addSubview:bottomLine];
        self.backgroundColor = RGB(0x24, 0xd9, 0xc6);
    }
    return self;
}

- (void)customNaviBackButton:(SEL)action target:(id) target
{
    
    if (!_backButton)
    {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
     //   [_backButton setImage:LOAD_IMAGE_USE_CACHE_INBUNDLE(DefaultBundle, @"btn_nav_back_nor.png") forState:UIControlStateNormal];
    }
    // 显示返回箭头，不显示文字了
    _backButton.frame = CGRectMake(-43, kNaviOrigenY, 120, 44);
    //_backButton.backgroundColor = [UIColor redColor];
    // 然后添加返回的处理方法
    [_backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_backButton];
}

- (void)customNaviLeftButton:(id)target action:(SEL) action image:(UIImage*) image
{
    if (!_backButton)
    {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    }
    [_backButton setImage:image forState:UIControlStateNormal];
//    _backButton.frame = CGRectMake(0, kNaviOrigenY, kNaviEditButtonWidth, kNaviEditButtonHeight);
    _backButton.frame = CGRectMake(10, kNaviOrigenY, image.size.width, kNaviEditButtonHeight);

    [_backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_backButton];
}

- (void)customNaviRightButton:(id)target action:(SEL) action image:(UIImage*) image
{
    if (_editBtn)
    {
        [_editBtn removeFromSuperview];
        _editBtn = nil;
    }
    _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_editBtn setImage:image forState:UIControlStateNormal];
    _editBtn.frame = CGRectMake(0, kNaviOrigenY, image.size.width, kNaviEditButtonHeight);
    _editBtn.left = kScreenWidth - image.size.width - 2*kNaviEditButtonMarginLeft;
    [_editBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_editBtn];
}

- (void)customNaviLeftButton:(id)target action:(SEL) action title:(NSString *)title
{
    if (_backButton)
    {
        [_backButton removeFromSuperview];
        _backButton = nil;
    }
    _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setTitle:title forState:UIControlStateNormal];
    [_backButton setTitleColor:[UIColor colorWithRGB:0xffffff] forState:UIControlStateNormal];
    _backButton.titleLabel.font = [UIFont systemFontOfSize:16];
  //  [_backButton setBackgroundColor:[UIColor redColor]];
   // _backButton.frame = CGRectMake(10, kNaviOrigenY, 30, 44);
    _backButton.frame = CGRectMake(0, kNaviOrigenY, kNaviEditButtonWidth, kNaviEditButtonHeight);
    [_backButton addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_backButton];
}

- (void)customNaviRightButton:(id)target action:(SEL) action title:(NSString *)title
{
    if (_editBtn)
    {
        [_editBtn removeFromSuperview];
        _editBtn = nil;
    }
    _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_editBtn setTitle:title forState:UIControlStateNormal];
    [_editBtn setTitleColor:[UIColor colorWithRGB:0xffffff] forState:UIControlStateNormal];
    _editBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    _editBtn.frame = CGRectMake(-kNaviEditButtonMarginLeft, kNaviOrigenY, kNaviEditButtonWidth, kNaviEditButtonHeight);
    _editBtn.left = kScreenWidth - kNaviEditButtonWidth;
    [_editBtn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_editBtn];
}


- (void)setTitle:(NSString *)title
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(kNaviTitleLabelMargin, kNaviOrigenY, kScreenWidth - kNaviTitleLabelMargin*2, kNaviTitleLabelHeight)];
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.backgroundColor = [UIColor clearColor];
        _titleLabel.font = [UIFont systemFontOfSize:16];
        _titleLabel.textColor = RGB(255, 255, 255);
        _titleLabel.userInteractionEnabled = YES;
        
        //        if (didTap)
        //        {
        //            UITapGestureRecognizer * tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:didTap];
        //            [titleLabel addGestureRecognizer:tapGesture];
        //        }
        
        [self addSubview:_titleLabel];
    }
    _titleLabel.text = title;
    
    [self bringSubviewToFront:_backButton];
    
    [self adjustTitleWidth];
}

- (void)adjustTitleWidth
{
    if (_titleLabel)
    {
        int margin = _backButton?kNaviTitleLabelMargin:66;
        if (self.editBtn && self.editBtn.hidden == NO) {
            margin = kScreenWidth - self.editBtn.frame.size.width + 5;
        }
        CGRect labelFrame = CGRectMake(margin, kNaviOrigenY, kScreenWidth - margin*2, kNaviTitleLabelHeight);
        [_titleLabel setFrame:labelFrame];
    }
}

-(UIButton *)editBtn
{
    if (_editBtn == nil)
    {
        _editBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _editBtn.tag = NavEditButtonTag;
        //        [_editBtn setBackgroundImage:VPGetImageByName(@"barbutton") forState:UIControlStateNormal];
        //        [_editBtn setBackgroundImage:VPGetImageByName(@"barbutton_press")  forState:UIControlStateHighlighted];
        [_editBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        _editBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        
        _editBtn.frame = CGRectMake(kScreenWidth-kNaviEditButtonMarginRight-kNaviEditButtonWidth, kNaviOrigenY+kNaviEditButtonMarginTop, kNaviEditButtonWidth, kNaviEditButtonHeight);
        _editBtn.hidden = YES;
        
        [self addSubview:_editBtn];
    }
    
    return _editBtn;
}

- (void)showNaviEditButton:(BOOL)bShow
{
    if (bShow)
    {
        self.editBtn.hidden = NO;
    }
    else
    {
        self.editBtn.hidden = YES;
    }
    
    [self adjustTitleWidth];
}
@end
